package com.example.t10v26;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Solution {
    List<Candy> candies = new ArrayList<Candy>();
    public int remaining = 0;

    public Solution(List<Candy> candies) {
        this.candies = candies;
        Collections.sort(this.candies, new Comparator<Candy>() {
            @Override
            public int compare(Candy c1, Candy c2) {
                return c2.price - c1.price;
            }
        });
    }


    public List<Candy> solve(int money) {
        int remainingMoney = money;
        for (int i = 0; i < candies.size() && remainingMoney > 0; i++) {
            Candy candy = candies.get(i);
            int maxAmount = Math.min(remainingMoney / candy.price, 1);
            if(candy.price == money && candies.size() - i > 2)
                continue;
            candy.amount = maxAmount;
            remainingMoney -= maxAmount * candy.price;
        }
        candies = candies.stream().filter(it -> it.amount > 0).toList();
        this.remaining = remainingMoney;
        return candies;
    }
}
